﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Globalization;
using AllenalaMente.State;
using System.Windows.Threading;

namespace AllenalaMente
{
    public partial class MainPage : PhoneApplicationPage
    {
        private GameState gameState = GameStateManager.GetState();
        Menu.InsertName insertName;
        DispatcherTimer timer;
        bool stopBlinking = true;


        public MainPage()
        {
            InitializeComponent();

            //// Set della lingua corrente
            //Locale.Strings.Culture = CultureInfo.CurrentCulture;

            ApplicationTitle.Text = "Carousel Team";
            PageTitle.Text = Locale.Strings.GameTitle;
            buttonHighScores.Content = Locale.Strings.Leaderboard;
            buttonCredits.Content = Locale.Strings.Credits;
            buttonOptions.Content = Locale.Strings.Options;
            buttonGameList.Content = Locale.Strings.GameList;

            if (gameState.playerName == "none")
            {
                insertName = new Menu.InsertName();
                LayoutRoot.Children.Add(insertName);
                insertName.buttonOk.Click += buttonOk_Click;
                insertName.textBoxName.KeyUp += textBoxName_KeyUp;
            }

            this.LayoutUpdated += MainPage_LayoutUpdated;

            Animation.FadeIn(this, 500);

            // Grstistco i template per lingua
            if (!buttonCredits.Content.Equals("Credits"))
                buttonStart.Template = (ControlTemplate)Resources["BtnStartTmpIta"];
            else
                buttonStart.Template = (ControlTemplate)Resources["BtnStartTmp"];

            // Attiva il timer collision
            timer = new DispatcherTimer();
            timer.Interval = TimeSpan.FromMilliseconds(1);
            timer.Tick += new EventHandler(timer_Tick);
            timer.Start();
        }

        #region Events

        void MainPage_LayoutUpdated(object sender, EventArgs e)
        {
            textBlockComix.Text = Locale.Strings.CiaoStart + "\n" + gameState.playerName;
        }

        // Start
        private void FlipStart_Completed(object sender, EventArgs e)
        {
            stopBlinking = false;
            imgprova.Opacity = 0;
            NavigationService.Navigate(new Uri("/AllenalaMente;component/Menu/GameSelection.xaml", UriKind.RelativeOrAbsolute));
        }

        private void buttonStart_Click(object sender, RoutedEventArgs e)
        {
            FlipStart.Begin();
        }

        // Options
        private void buttonOptions_Click(object sender, RoutedEventArgs e)
        {

            NavigationService.Navigate(new Uri("/AllenalaMente;component/Menu/OptionsMenu.xaml", UriKind.RelativeOrAbsolute));
        }

        // Credits
        private void buttonCredits_Click(object sender, RoutedEventArgs e)
        {
            NavigationService.Navigate(new Uri("/AllenalaMente;component/Menu/CreditsMenu.xaml", UriKind.RelativeOrAbsolute));
        }

        // Achievements
        private void buttonAchievements_Click(object sender, RoutedEventArgs e)
        {
            NavigationService.Navigate(new Uri("/AllenalaMente;component/Menu/AchievementsMenu.xaml", UriKind.RelativeOrAbsolute));
        }

        // Classifica
        private void buttonHighScores_Click(object sender, RoutedEventArgs e)
        {
            NavigationService.Navigate(new Uri("/AllenalaMente;component/Pages/LeaderBoardPage.xaml", UriKind.RelativeOrAbsolute));
        }

        // Lista Completa dei Giochi
        private void buttonGameList_Click(object sender, RoutedEventArgs e)
        {
            NavigationService.Navigate(new Uri("/AllenalaMente;component/Menu/GamesListTest.xaml", UriKind.RelativeOrAbsolute));
        }

        // Insert Name - Enter
        void textBoxName_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter) buttonOk_Click(this, null);
        }

        // Insert Name - OkButton
        void buttonOk_Click(object sender, RoutedEventArgs e)
        {
            if (insertName.textBoxName.Text != "" && insertName.textBoxName.Text.Length > 3)
            {
                gameState.playerName = insertName.textBoxName.Text;
                LayoutRoot.Children.Remove(insertName);
                insertName.buttonOk.Click -= buttonOk_Click;

                textBlockComix.Text = Locale.Strings.CiaoStart + "\n " + gameState.playerName;
            }
            else
            {
                insertName.textBlockWarning.Visibility = Visibility.Visible;
                insertName.textBlockWarning.Text = Locale.Strings.StartWarningText;
            }

        }

        #endregion

        #region animation update


        double x = 1;
        void timer_Tick(object sender, EventArgs e)
        {
            if (stopBlinking)
            {
                x -= 0.003;
                if (x < 0.4)
                    x = 1;
                imgprova.Opacity = x;
            }


            if (imgprova.Opacity == 0)
            {
                stopBlinking = true;
                FlipEnd.Begin();
            }


        }
        #endregion

        private void FlipEnd_Completed(object sender, EventArgs e)
        {
            timer.Start();
        }


    }
}
